package org.jypj.dev.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jypj.dev.constant.BaseConstant;
import org.jypj.dev.constant.URLConstant;
import org.jypj.dev.vo.hxVo.PushMsg;
import org.jypj.dev.vo.hxVo.PushObject;
import org.jypj.dev.vo.hxVo.Token;
import org.jypj.dev.model.User;

import java.util.Date;
import java.util.Set;

/**
 * Created by ChenYu on 2017-06-05.
 */
public class HxUtil {

    private static Token token;//环信的token

    public static void main(String[] args) {
        User user = new User();
        user.setUserNum("1044");
        saveUser(user);
    }

    /**
     * 给单个用户发送消息
     *
     * @param userNum
     * @param pushObject
     * @return
     */
    public static int sendMsgToOneUser(String userNum, PushObject pushObject) {
        PushMsg pushMsg = new PushMsg(pushObject);
        pushMsg.getTarget().add(userNum);
        try {
            sendMsg(pushMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    /**
     * 发送消息给用
     *
     * @param users
     * @param pushObject
     * @return
     */
    public static int sendMsgToSomeUsers(Set<String> users, PushObject pushObject) {
        PushMsg pushMsg = new PushMsg(pushObject);
        pushMsg.setTarget(users);
        try {
            sendMsg(pushMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    /**
     * 发送消息
     *
     * @param pushMsg
     * @return
     * @throws Exception
     */
    public static String sendMsg(PushMsg pushMsg) throws Exception {
        System.out.println(pushMsg.toString());
        String s = HttpUtils.sendHxPostRequestByForm(URLConstant.HX_SEND_MESSAGE, pushMsg.toString(), getTokenStr());
        System.out.println(s);
        return "";
    }

    /**
     * 得到token
     *
     * @return
     */
    public static synchronized String getTokenStr() {
        long currentTime = new Date().getTime();
        if (token == null || (currentTime - token.getCreateTime()) > token.getExpires_in()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("grant_type", "client_credentials");
            jsonObject.put("client_id", BaseConstant.HX_CLIENT_ID);
            jsonObject.put("client_secret", BaseConstant.HX_CLIENT_SECRET);
            try {
                String s = HttpUtils.sendPostRequestByForm(URLConstant.HX_GET_TOKEN_URL, jsonObject.toJSONString());
                Token token1 = JSON.parseObject(s, Token.class);
                token1.setCreateTime(currentTime);
                token = token1;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return token.getAccess_token();
    }

    /**
     * 查询用户是否存在
     *
     * @param userNum
     */
    public static boolean queryUser(String userNum) {
        try {
            HttpUtils.sendHxGetRequestByForm(URLConstant.HX_GET_USER_URL + userNum, getTokenStr());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 环信注册用户
     *
     * @param user
     */
    public static void saveUser(User user) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", user.getUserNum());
        jsonObject.put("password", "abc" + user.getUserNum());
        //注册环信用户
        System.out.println("新注册用户" + jsonObject.toJSONString());
        try {
            String s = HttpUtils.sendPostRequestByForm(URLConstant.HX_SAVE_USER_URL, jsonObject.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param groupStr
     * @throws Exception
     */
    public static void createHxGroup(String groupStr) throws Exception {
        String s = HttpUtils.sendHxPostRequestByForm(URLConstant.HX_CREATE_GROUP, groupStr, getTokenStr());
        System.out.println(s);
    }
}
